<template>
  <div id="app">
    <nav class="navbar">
      <div class="nav-container">
        <router-link to="/" class="nav-brand"> 📊 数学学习可视化 </router-link>
        <div class="nav-right">
          <div class="nav-links">
            <router-link to="/" class="nav-link">首页</router-link>
            <router-link to="/learn" class="nav-link">
              学习中心
              <span v-if="!userStore.isAuthenticated" class="preview-badge">预览版</span>
            </router-link>
            <router-link to="/practice" class="nav-link">
              练习中心
              <span v-if="!userStore.isAuthenticated" class="preview-badge">预览版</span>
            </router-link>
            <!-- <router-link to="/progress" class="nav-link">学习进度</router-link> -->
          </div>
        </div>

        <!-- 用户信息弹出框 -->
        <el-popover
          v-if="userStore.isAuthenticated"
          placement="bottom-end"
          :width="280"
          trigger="click"
          popper-class="user-popover"
          @show="handleShow"
          @hide="handleHide"
        >
          <template #reference>
            <div class="user-area">
              <img class="avatar" :src="userStore.getUserAvatar" alt="avatar" />
              <span class="nickname">{{ userStore.user?.username }}</span>
              <el-icon :class="[isDropdownOpen ? 'dropdown-icon' : '']"><ArrowDown /></el-icon>
            </div>
          </template>

          <div class="user-popover-content">
            <!-- 用户基本信息 -->
            <div class="user-info">
              <img :src="userStore.getUserAvatar" class="popover-avatar" />
              <div class="user-details">
                <h4>{{ userStore.user?.username }}</h4>
                <p>{{ userStore.user?.email || '未设置邮箱' }}</p>
                <div class="user-stats">
                  <span class="stat-item">
                    <el-icon><Clock /></el-icon>
                    {{ progressStore.learningProgress.totalStudyTime }}分钟
                  </span>
                  <span class="stat-item">
                    <el-icon><Check /></el-icon>
                    {{ progressStore.learningProgress.completedQuestions }}题
                  </span>
                </div>
              </div>
            </div>

            <el-divider />

            <!-- 菜单选项 -->
            <div class="menu-items">
              <router-link to="/profile" class="menu-item">
                <el-icon><User /></el-icon>
                <span>个人中心</span>
              </router-link>

              <router-link to="/progress" class="menu-item">
                <el-icon><TrendCharts /></el-icon>
                <span>学习进度</span>
                <el-badge
                  :value="progressStore.learningProgress.completedQuestions"
                  class="progress-badge"
                  :hidden="progressStore.learningProgress.completedQuestions === 0"
                />
              </router-link>

              <div class="menu-item" @click="handleLogout">
                <el-icon><SwitchButton /></el-icon>
                <span>退出登录</span>
              </div>
            </div>
          </div>
        </el-popover>

        <div class="user-area" v-else>
          <router-link to="/login" class="nav-link">登录</router-link>
        </div>
      </div>
    </nav>

    <main class="main-content">
      <router-view />
    </main>

    <footer class="footer">
      <div class="footer-content">
        <p>&copy; 2025 数学学习可视化项目. 让数学学习变得简单有趣.</p>
      </div>
    </footer>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref } from 'vue'
import { useUserStore, useAuthStore, useProgressStore } from '@/stores'
import { ArrowDown, User, TrendCharts, SwitchButton, Clock, Check } from '@element-plus/icons-vue'

const userStore = useUserStore()
const authStore = useAuthStore()
const progressStore = useProgressStore()
// icon-down 图标旋转
const isDropdownOpen = ref(false)

// 处理登出
const handleLogout = () => {
  authStore.logout()
  progressStore.resetProgress()
}

const handleShow = () => {
  console.log('handleShow')
  isDropdownOpen.value = true
}

const handleHide = () => {
  console.log('handleHide')
  isDropdownOpen.value = false
}

onMounted(async () => {
  // 自动登录（从本地存储恢复）
  await authStore.autoLogin()

  // 如果用户已登录，获取学习进度
  if (userStore.isAuthenticated && userStore.user?.id) {
    await progressStore.fetchUserProgress(userStore.user.id)
  }
})
</script>

<style>
/* 全局样式 */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family:
    -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
  background: #f5f5f5;
  color: #333;
  line-height: 1.6;
}

#app {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.navbar {
  background: white;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  position: sticky;
  top: 0;
  z-index: 100;
}

.nav-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 60px;
}

.nav-right {
  display: flex;
  align-items: center;
  gap: 16px;
}

.nav-brand {
  font-size: 1.5rem;
  font-weight: bold;
  color: #333;
  text-decoration: none;
  transition: color 0.3s ease;
}

.nav-brand:hover {
  color: #2196f3;
}

.nav-links {
  display: flex;
  gap: 24px;
}

.user-area {
  display: flex;
  align-items: center;
  gap: 8px;
}

.avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  object-fit: cover;
  box-shadow: 0 0 0 2px #e3f2fd;
}

.nickname {
  color: #333;
  font-weight: 500;
}

.dropdown-icon {
  margin-left: 4px;
  font-size: 12px;
  color: #999;
  transition: transform 0.3s ease;
}

.dropdown-icon {
  transform: rotate(180deg);
}

/* 用户弹出框样式 */
:deep(.user-popover) {
  padding: 0;
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.12);
}

.user-popover-content {
  padding: 0;
}

.user-info {
  padding: 20px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border-radius: 12px 12px 0 0;
}

.popover-avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  border: 3px solid rgba(255, 255, 255, 0.3);
  margin-bottom: 12px;
}

.user-details h4 {
  margin: 0 0 4px 0;
  font-size: 18px;
  font-weight: 600;
}

.user-details p {
  margin: 0 0 12px 0;
  opacity: 0.9;
  font-size: 14px;
}

.user-stats {
  display: flex;
  gap: 16px;
}

.stat-item {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
  opacity: 0.9;
}

.menu-items {
  padding: 16px;
}

.menu-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  margin-bottom: 4px;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.2s ease;
  text-decoration: none;
  color: #333;
}

.menu-item:hover {
  background-color: #f5f7fa;
  transform: translateX(4px);
}

.menu-item:last-child {
  margin-bottom: 0;
  color: #f56c6c;
}

.menu-item:last-child:hover {
  background-color: #fef0f0;
}

.menu-item .el-icon {
  font-size: 18px;
}

.progress-badge {
  margin-left: auto;
}

.nav-link {
  color: #666;
  text-decoration: none;
  font-weight: 500;
  transition: color 0.3s ease;
  padding: 8px 12px;
  border-radius: 6px;
}

.nav-link:hover {
  color: #2196f3;
  background: #f0f8ff;
}

.nav-link.router-link-active {
  color: #2196f3;
  background: #e3f2fd;
}

.preview-badge {
  display: inline-block;
  background: #ff9800;
  color: white;
  font-size: 0.7rem;
  padding: 2px 6px;
  border-radius: 10px;
  margin-left: 6px;
  font-weight: normal;
  vertical-align: middle;
}

.main-content {
  flex: 1;
  /* padding: 20px 0; */
}

.footer {
  background: #333;
  color: white;
  padding: 20px 0;
  margin-top: auto;
}

.footer-content {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
  text-align: center;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .nav-container {
    flex-direction: column;
    height: auto;
    padding: 16px 20px;
  }

  .nav-links {
    margin-top: 16px;
    flex-wrap: wrap;
    justify-content: center;
  }
}
</style>
